import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { h } from "vue"
import { Icon } from "/@/components/Icon"
import { ajaxGetDictItems } from "/@/api/iam/menu"
import { render } from "/@/utils/common/renderUtils"
import dictEnum from "/@/enums/dictEnum"
import { useUserStore } from "/@/store/modules/user"
import { NamespaceCodeEnum } from "/@/enums/iamEnum"
const userStore = useUserStore()
const selectTenant = userStore.getSelectTenant
const isDir = (type) => type === "0"
const isMenu = (type) => type === "1"
const isButton = (type) => type === "2"
export const isPermPolicy = (type) => type === "3"

// 定义可选择的组件类型
export enum ComponentTypes {
  Default = "layouts/default/index",
  IFrame = "sys/iframe/FrameBlank",
}

export const columns: BasicColumn[] = [
  {
    title: "菜单名称",
    dataIndex: "name",
    width: 230,
    align: 'left',
    slot: 'firstColumn',
  },
  {
    title: "菜单类型",
    dataIndex: "menuTypeText",
    width: 80,
    // customRender: ({text}) => {
    //   return  render.renderDict(text, 'menu_type')
    // }
  },
  {
    title: "权限标识",
    dataIndex: "apiActions",
    width: 120,
    slot: "apiActions"
  },
  {
    title: "组件",
    dataIndex: "component",
    align: "left",
    width: 150,
  },
  {
    title: "路径",
    dataIndex: "path",
    align: "left",
    width: 150,
  },
  {
    title: "状态",
    dataIndex: "status",
    customRender: ({ text }) => {
      let color = "green"
      if (text == 2) {
        color = "red"
      }
      return render.renderTag(render.renderDict(text, dictEnum.DataAvailable), color)
    },
    width: 60,
  },
  {
    title: "排序",
    dataIndex: "sortNum",
    width: 90,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "name",
    label: "菜单名称",
    component: "Input",
  },
  {
    field: "checkMenu",
    component: "Select",
    label: "校验菜单",
    show: userStore.getIsSysAdminTenant,
    helpMessage: ["筛选出菜单类型为权限且存在无效的权限标识", "需要进行资源同步, 才能筛选最新的无效权限标识菜单"],
    colProps: {
      span: 4,
    },
    defaultValue: null,
    componentProps: {
      allowClear: true,
      options: [
        {
          label: "是",
          value: true,
          key: "1",
        },
        {
          label: "否",
          value: false,
          key: "2",
        },
      ],
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "id",
    field: "id",
    component: "Input",
    defaultValue: null,
    show: false,
  },
  {
    label: "apiNamespaceCode",
    field: "apiNamespaceCode",
    component: "Input",
    show: false,
  },
  {
    label: "addonId",
    field: "addonId",
    component: "Input",
    defaultValue: null,
    show: false,
  },
  {
    field: "menuType",
    label: "菜单类型",
    component: "RadioButtonGroup",
    defaultValue: "0",
    ifShow: ({ values }) => !values.addonId,
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: "一级菜单", value: "0" },
          { label: "子菜单", value: "1" },
          { label: "按钮/权限", value: "2" },
        ],
        onChange: (e) => {
          const { updateSchema, clearValidate } = formActionType
          // const label = isButton(e) ? '按钮/权限' : '菜单名称';
          const label = isButton(e) ? "菜单名称" : "菜单名称"
          //清除校验
          clearValidate()
          updateSchema([
            {
              field: "name",
              label: label,
            },
            {
              field: "url",
              required: !isButton(e),
            },
          ])
        },
      }
    },
  },
  {
    field: "namespaceCode",
    label: "可见范围",
    component: "JDictSelectTag",
    defaultValue: null,
    required: true,
    ifShow: ({ values }) => (!values.addonId && values.apiNamespaceCode === NamespaceCodeEnum.APP_GOVERN_HUB),
    componentProps: {
      type: 'radio',
      showSearch: true,
      allowClear: true,
      dictType: "SysMenuNamespaceCode",
      placeholder: "请选择可见范围",
    }
  },
  {
    field: "name",
    label: "菜单名称",
    component: "Input",
    required: true,
    ifShow: ({ values }) => values.mergeApiAction,
  },
  {
    field: "apiActions",
    label: "权限标识",
    component: "ApiSelect",
    componentProps: {
      mode: "tags",
      dropdownStyle: { maxHeight: "100vh", overflow: "auto" },
    },
    ifShow: ({ values }) => !values.addonId && (isButton(values.menuType)),
  },
  {
    field: "mergeApiAction",
    label: "合并权限标识",
    helpMessage: [
      "开启: 只会生成一条菜单记录",
      "关闭: 如果有多条权限标识则会生成多个菜单记录"
    ],
    component: "Switch",
    defaultValue: true,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => values.apiActions?.length > 1,
  },
  {
    field: "parentId",
    label: "上级菜单",
    component: "MyTreeSelect",
    required: false,
    componentProps: {
      fieldNames: {
        title: "name",
        key: "id",
        value: "id",
      },
    },
    ifShow: ({ values }) => !values.addonId && !isDir(values.menuType),
  },
  {
    field: "path",
    label: "访问路径",
    component: "Input",
    dynamicDisabled: ({ values }) => {
      return !!values.id && values.addonId
    },
    helpMessage: [
      "1. 如果是内嵌或者外部地址, 则该值需要填写名称, 而不能是http(s)://xxx, eg: doc", "如果是外部地址, 需要选择打开方式为外部",
      '2. 如果不以 / 开头, 则访问路径继承父级菜单路径, 若指定 /, 则指定路径同访问路径'
    ],
    required: true,
    defaultValue: "#",
    ifShow: ({ values }) => !(values.component === ComponentTypes.IFrame && values.internalOrExternal),
  },
  {
    field: "component",
    label: "前端组件",
    component: "Input",
    componentProps: {
      placeholder: "请输入前端组件",
    },
    helpMessage: [
      '一、组件路径说明：',
      '　1、自定义 Vue 组件路径，一般不需要填写，默认是根据 “链接地址” 进行自动生成',
      '　2、当 “链接地址” 或 “路由地址” 与 Vue 组件路径匹配规则不一致的时候配置',
      '二、也可以设置内置组件名称：',
      '　1、填写 IFRAME 则强制使用 iframe 打开链接',
      '　2、填写 LAYOUT 将不在 Beetl 视图中显示菜单项',
      '　3、填写 BLANK 则不显示主框架（仅顶部菜单设置即可）',
    ],
    // defaultValue: 'layouts/RouteView',
    defaultValue: "layout",
    dynamicDisabled: ({ values }) => !!values.id && values.addonId,
    required: ({ values }) => !isButton(values.menuType),
    ifShow: ({ values }) => !isButton(values.menuType),
  },
  {
    label: '组件参数',
    field: 'params',
    helpMessage: [
      '可选，给组件传参，请填写 JSON 格式，前端通过定义 props 获取',
      "举例：链接地址填写：/test/params   组件参数填写：{aa:'aa1',bb:'bb2'}",
      'Vue：const props = defineProps({ aa: String, bb: String })',
    ],
    component: 'Input',
    componentProps: {
      maxlength: 500,
    },
  },
  // {
  //   field: "frameSrc",
  //   label: "Iframe地址",
  //   dynamicDisabled: ({ values }) => !!values.id && values.addonId,
  //   component: "Input",
  //   rules: [
  //     { required: true, message: "请输入Iframe地址" },
  //     { type: "url", message: "请输入正确的url地址" },
  //   ],
  //   ifShow: ({ values }) => !isButton(values.menuType) && values.component === ComponentTypes.IFrame,
  // },
  {
    field: "redirect",
    label: "默认跳转地址",
    dynamicDisabled: ({ values }) => !!values.id && values.addonId,
    component: "Input",
    ifShow: ({ values }) => isDir(values.menuType),
  },
  {
    field: "permsType",
    label: "授权策略",
    component: "RadioGroup",
    defaultValue: "1",
    helpMessage: ["可见/可访问(授权后可见/可访问)", "可编辑(未授权时禁用)"],
    componentProps: {
      options: [
        { label: "可见/可访问", value: "1" },
        { label: "可编辑", value: "2" },
      ],
    },
    ifShow: ({ values }) => isButton(values.menuType),
  },
  {
    field: "status",
    label: "状态",
    component: "RadioGroup",
    defaultValue: "1",
    componentProps: {
      options: [
        { label: "有效", value: "1" },
        { label: "无效", value: "0" },
      ],
    },
    ifShow: ({ values }) => isButton(values.menuType),
  },
  {
    field: "icon",
    label: "菜单图标",
    component: "IconPicker",
    ifShow: ({ values }) => !isButton(values.menuType),
  },
  {
    field: "sortNum",
    label: "排序",
    component: "InputNumber",
    defaultValue: 1,
  },
  {
    field: "route",
    label: "是否路由菜单",
    component: "Switch",
    defaultValue: true,
    colProps: { span: 24 },
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "hideMenu",
    label: "隐藏菜单",
    component: "Switch",
    colProps: { span: 24 },
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "hideTab",
    label: "隐藏Tab",
    helpMessage: "隐藏tab后, 则激活的是父级tab, 所以不能将父级tab也进行隐藏",
    component: "Switch",
    colProps: { span: 24 },
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "keepAlive",
    label: "是否缓存路由",
    colProps: { span: 24 },
    component: "Switch",
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "alwaysShow",
    label: "聚合路由",
    component: "Switch",
    colProps: { span: 24 },
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "external",
    label: "打开方式",
    component: "Switch",
    colProps: { span: 24 },
    defaultValue: false,
    componentProps: {
      checkedChildren: "外部",
      unCheckedChildren: "内部",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
  {
    field: "isMainOut",
    label: "主框架外",
    component: "Switch",
    colProps: { span: 24 },
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
    ifShow: ({ values }) => !values.addonId && !isButton(values.menuType),
  },
]

export const dataRuleColumns: BasicColumn[] = [
  {
    title: "规则名称",
    dataIndex: "ruleName",
    width: 150,
  },
  {
    title: "规则编码",
    dataIndex: "ruleCodeText",
    width: 100,
  },
  {
    title: "规则字段",
    dataIndex: "ruleColumn",
    width: 100,
  },
  {
    title: "规则值",
    dataIndex: "ruleValue",
    width: 100,
  },
]

export const dataRuleSearchFormSchema: FormSchema[] = [
  {
    field: "ruleName",
    label: "规则名称",
    component: "Input",
    colProps: { span: 6 },
  },
  {
    field: "ruleValue",
    label: "规则值",
    component: "Input",
    colProps: { span: 6 },
  },
]

export const dataRuleFormSchema: FormSchema[] = [
  {
    label: "id",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    field: "ruleName",
    label: "规则名称",
    component: "Input",
    required: true,
  },
  {
    field: "ruleColumn",
    label: "规则字段",
    helpMessage: "代码中实体类的字段名",
    component: "Input",
    ifShow: ({ values }) => {
      const ruleConditions = values.ruleConditions
      return (
        ruleConditions !== "data_my" &&
        ruleConditions !== "data_my_current_dept" &&
        ruleConditions !== "data_my_all_dept" &&
        ruleConditions !== "USE_SQL_RULES"
      )
    },
  },
  {
    field: "ruleCode",
    label: "规则编码",
    required: true,
    component: "ApiSelect",
    componentProps: {
      api: ajaxGetDictItems,
      params: { code: "data_rule_code" },
      labelField: "label",
      valueField: "value",
      getPopupContainer: (node) => document.body,
    },
  },
  {
    field: "ruleConditions",
    label: "条件规则",
    required: true,
    component: "ApiSelect",
    componentProps: {
      api: ajaxGetDictItems,
      params: { code: "rule_conditions" },
      labelField: "label",
      valueField: "value",
      getPopupContainer: (node) => document.body,
    },
  },
  {
    field: "ruleValue",
    label: "规则值",
    component: "Input",
    // required: true,
    ifShow: ({ values }) => {
      const ruleConditions = values.ruleConditions
      return ruleConditions !== "data_my" && ruleConditions !== "data_my_current_dept" && ruleConditions !== "data_my_all_dept"
    },
  },
  {
    field: "status",
    label: "状态",
    component: "RadioButtonGroup",
    defaultValue: "1",
    componentProps: {
      options: [
        { label: "无效", value: "0" },
        { label: "有效", value: "1" },
      ],
    },
  },
]
